package model

import (
	"time"

	"github.com/google/uuid"
	"gorm.io/gorm"
)

type Client struct {
	ID        string    `json:"id"`
	Enabled   bool      `json:"enabled"`
	Name      string    `json:"name"`
	Email     string    `json:"email"`
	Phone     string    `json:"phone"`
	Address   string    `json:"address"`
	Company   string    `json:"company"`
	Remarks   string    `json:"remarks"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

func CreateClient(db *gorm.DB, c *Client) error {
	if c.ID == "" {
		c.ID = uuid.NewString()
	}
	return db.Create(c).Error
}

func UpdateClient(db *gorm.DB, c *Client) error {
	return db.Model(&Client{ID: c.ID}).Updates(*c).Error
}

func DeleteClient(db *gorm.DB, id string) error {
	return db.Delete(&User{}, id).Error
}

func ListClient(db *gorm.DB, filters map[string]interface{}) ([]Client, error) {
	cc := []Client{}
	return cc, db.Where(filters).Find(&cc).Error
}

func FindClient(db *gorm.DB, id string) (*Client, error) {
	c := &Client{}
	err := db.First(c, id).Error
	return c, err
}

func FindClientByName(db *gorm.DB, name string) (*Client, error) {
	c := &Client{}
	err := db.Where("name = ?", name).First(c).Error
	return c, err
}
